package cn.hengzhu.main_manager.web.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;

import cn.hengzhu.main_manager.repository.dao.ManageCompMapper;
import cn.hengzhu.main_manager.repository.domain.ManageComp;
import cn.hengzhu.main_manager.repository.domain.ManageUser;
import cn.hengzhu.main_manager.utils.UserContext;
import cn.hengzhu.main_manager.utils.ann.MoblieUrl;
import cn.hengzhu.main_manager.utils.qrCode.MatrixToImageWriter;


/**
 * @author 刘成
 * @since 2018年9月4日
 * 用户资金账户管理
 */
@Controller
@RequestMapping("/userAmount")
public class AmountController {
	@Autowired
	private ManageCompMapper manageCompMapper;
	@Autowired
	private HttpServletRequest request;
	
	@MoblieUrl
	@RequestMapping("/index")
	public String getComp(Model model) {
		ManageUser user = UserContext.getUser(); 
		Integer compId = user.getCompId();
		System.out.println(compId);
		ManageComp compByCompId = manageCompMapper.selectByPrimaryKey(compId);
		if(compByCompId!=null){
			String aliOpenid = compByCompId.getAliOpenid();
			String weixinOpenid = compByCompId.getWeixinOpenid();
			BigDecimal compBalance = compByCompId.getCompBalance();
			String compBalanceStr = compBalance.toString();
			model.addAttribute("aliOpenid", aliOpenid);
			model.addAttribute("weixinOpenid", weixinOpenid);
			model.addAttribute("compBalanceStr", compBalanceStr);
		}
		
		//查询微信的收款人账户
		//查询余额
		//查询支付宝的收款人的账户
		return request.getAttribute("moblie")+"userAmount";
	}
	
	/**
	 * @author 刘成
	 * @since 2018年9月10日
	 * @return
	 * 绑定微信收款账户的二维码接口
	 */
	@RequestMapping("/getWeixinBinder")
	@ResponseBody
	public ResponseEntity<byte[]> getWeixinBinder() throws Exception{
		Map<String,String> map=new HashMap<>();
		ManageUser user = UserContext.getUser();
		String loginName = user.getLoginName();
		String passWord = user.getPassWord();
		map.put("loginName", loginName);
		map.put("passWord", passWord);
		String jsonString = JSONObject.toJSONString(map);
		String url =jsonString;
		Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		hints.put(EncodeHintType.MARGIN, 2);
		
		BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);	
		BufferedImage Qrimage = MatrixToImageWriter.toBufferedImage(bitMatrix);// 二维码
		
		ByteArrayOutputStream bs = new ByteArrayOutputStream();
		ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
		ImageIO.write(Qrimage, "jpg", imOut);
		bs.close();
		imOut.close();
		byte[] byteArray = bs.toByteArray();
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.set("Content-Type", "image/jpg");
		return new ResponseEntity<byte[]>(byteArray, responseHeaders, HttpStatus.CREATED);
	}
	
}
